<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文章管理</title>
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/css/layui.css">
    <script src="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/layui.js" charset="utf-8"></script>
</head>

<body>
    <div style="padding: 15px;margin: 15px;background-color: #fff;">
        <!-- 表单 -->
        <form class="layui-form">
            {:token_field()}
            <div class="layui-form-item">
                <label class="layui-form-label">分类名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" value="" lay-verify="required" placeholder="请输入" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上级分类</label>
                <div class="layui-input-inline">
                    <select name="pid" lay-verify="required">
                        <option value="0" {if $pid==0} selected{/if}>&mdash; 顶级分类 &mdash;</option>
                        {foreach $cates as $cate}
                        <option value="{$cate['id']}" {if $pid==$cate['id']} selected{/if}>{$cate['name']|raw}
                        </option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-inline">
                    <input type="number" name="sort" value="0" lay-verify="required|number" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="hidden" name="status" value="1">
                    <input type="checkbox" name="status" value="0" checked lay-text="开启|禁用" lay-skin="switch">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>

    </div>

    <script>
        layui.use(['form'], function () {
            var form = layui.form
                , $ = layui.jquery;

            // 获取应用、控制器和方法
            var module = '{:app("http")->getName()}'
                , controller = '{$Request.controller}'
                , action = '{$Request.action}';

            // 监听提交
            form.on('submit(submit)', function (data) {
                $.post('/' + module + '/' + controller + '/cate_save', data.field, function (res) {
                    if (res.code) return layer.alert(res.msg, { icon: 2 });
                    layer.alert(res.msg, { icon: 1 });
                    setTimeout(() => {
                        var parentIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(parentIndex);
                        parent.layui.table.reload('dataTable');
                    }, 1000)
                }, 'json');
                return false;
            });

        });
    </script>
</body>

</html>